package com.younion.common.paginator;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.TagSupport;

import com.younion.common.system.SystemConfig;

/**
 * 分页tag,生成分页的跳转标识
 * 
 * @author wwz
 */
public class PaginatorTag extends TagSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String name; // 分页器的名称

	private String scope = "request"; // 分页器所在位置page/request/session/application

	private static final Map scopeMap = new HashMap(); // 属性范围Map

	final SystemConfig sm = SystemConfig.getInstance();
	String ps = sm.getPropValue("pageSize");

	static {
		// 初始化属性范围Map
		scopeMap.put("page", new Integer(PageContext.PAGE_SCOPE));
		scopeMap.put("request", new Integer(PageContext.REQUEST_SCOPE));
		scopeMap.put("session", new Integer(PageContext.SESSION_SCOPE));
		scopeMap.put("application", new Integer(PageContext.APPLICATION_SCOPE));
	}

	public int doEndTag() throws JspTagException {

		if (ps == null || "".equals(ps)) {
			ps = "15";
		}

		Paginator paginator = (Paginator) pageContext.getAttribute(name, ((Integer) scopeMap
				.get(scope)).intValue());
		String path = "/younionmember";

		JspWriter out = pageContext.getOut();
		StringBuffer buffer = new StringBuffer(1000);
		if (paginator == null) {
			paginator = new Paginator();
		}
		int totalPage = paginator.getTotalPage();
		try {
			// if (paginator.getTotalCount() > 0) {// 有记录
			buffer.append("<script language=\"JavaScript\">\n");
			buffer.append("<!--\n");
			buffer.append("function GotoPage(id) {\n");
			buffer.append("document.all.pageRequest.value=1;\n");
			buffer.append("if(id<=0){id=1;}\n");
			buffer.append("document.all.pageIndex.value=id;\n");
			buffer.append("document.forms[0].submit();\n");
			buffer.append("}\n");

			buffer.append("function checkPageSize(page,size,total){\n");
			buffer.append("var reg = /[^0-9]/;\n");
			buffer.append("if(size.match(reg) != null){\n");
			buffer.append("alert('每页记录数请使用数字');\n");
			buffer.append("document.all.pageSize.focus();}\n");
			buffer.append("if(size<1){\n");
			buffer.append("document.all.pageSize.value=" + ps + ";}\n");
			buffer.append("if(size>5000){\n");
			buffer.append("document.all.pageSize.value=5000;}\n");
			buffer.append("var newsize = document.all.pageSize.value;\n");
			buffer.append("checkPage(page,newsize,total);\n");
			buffer.append("}\n");

			buffer.append("function goOver(){\n");
			buffer.append("document.all.goPage.src='" + path + "/images/page/go2.gif';\n");
			buffer.append("return false;\n");
			buffer.append("}\n");
			buffer.append("function goOut(){\n");
			buffer.append("document.all.goPage.src='" + path + "/images/page/go.gif';\n");
			buffer.append("return false;\n");
			buffer.append("}\n");

			buffer.append("function firstOver(){\n");
			buffer.append("document.all.first.src='" + path + "/images/page/first2.gif';\n");
			buffer.append("return false;\n");
			buffer.append("}\n");
			buffer.append("function firstOut(){\n");
			buffer.append("document.all.first.src='" + path + "/images/page/first.gif';\n");
			buffer.append("return false;\n");
			buffer.append("}\n");

			buffer.append("function prevOver(){\n");
			buffer.append("document.all.prev.src='" + path + "/images/page/prev2.gif';\n");
			buffer.append("return false;\n");
			buffer.append("}\n");
			buffer.append("function prevOut(){\n");
			buffer.append("document.all.prev.src='" + path + "/images/page/prev.gif';\n");
			buffer.append("return false;\n");
			buffer.append("}\n");

			buffer.append("function nextOver(){\n");
			buffer.append("document.all.next.src='" + path + "/images/page/next2.gif';\n");
			buffer.append("return false;\n");
			buffer.append("}\n");
			buffer.append("function nextOut(){\n");
			buffer.append("document.all.next.src='" + path + "/images/page/next.gif';\n");
			buffer.append("return false;\n");
			buffer.append("}\n");

			buffer.append("function lastOver(){\n");
			buffer.append("document.all.last.src='" + path + "/images/page/last2.gif';\n");
			buffer.append("return false;\n");
			buffer.append("}\n");
			buffer.append("function lastOut(){\n");
			buffer.append("document.all.last.src='" + path + "/images/page/last.gif';\n");
			buffer.append("return false;\n");
			buffer.append("}\n");

			buffer.append("function checkPage(page,size,total){\n");
			buffer.append("var reg = /[^0-9]/;\n");
			buffer.append("if(page.match(reg) != null){\n");
			buffer.append("alert('跳转页数请使用数字');\n");
			buffer.append("document.all.pageIndex.focus();}\n");
			buffer.append("var num = total/size;  \n");
			buffer.append("var nums = Math.round(num+0.4999999);\n");
			buffer.append("if(page<1){\n");
			buffer.append("document.all.pageIndex.value=1;}\n");
			buffer.append("if(page > nums){\n");
			buffer.append("document.all.pageIndex.value=nums;}\n");
			buffer.append("}\n");

			buffer.append("//-->\n");
			buffer.append("</script>\n");
			buffer
					.append("<script language=\"JavaScript\" event=\"onkeydown\" for=\"document\">\n");
			buffer.append("if(event.srcElement.name=='pageSize'){\n");
			buffer
					.append("if(((event.keyCode>=48)&&(event.keyCode<=57))||((event.keyCode>=96)&&(event.keyCode<=105))||(event.keyCode==8)||(event.keyCode==46)||(event.keyCode==13)||(event.keyCode==9)){\n");
			buffer.append("return true;\n");
			buffer.append("}else{return false;}}");
			buffer.append("if(event.srcElement.name=='pageIndex'){\n");
			buffer
					.append("if(((event.keyCode>=48)&&(event.keyCode<=57))||((event.keyCode>=96)&&(event.keyCode<=105))||(event.keyCode==8)||(event.keyCode==46)||(event.keyCode==13)||(event.keyCode==9)){\n");
			buffer.append("return true;\n");
			buffer.append("}else{return false;}}");
			buffer.append("</script>\n");
			// 以上为页面跳转的JavaScript方法

			buffer.append("<table width='100%' border='0' align='right'>\n");
			buffer.append("<tr>\n");
			buffer.append("<td width='5%' align='center'></td>\n");
			buffer.append("<td width='10%' align='right'>\n" + paginator.getTotalCount() + "条记录");
			buffer.append("</td>\n");
			buffer.append("<td width='12%' align='center'>\n");
			buffer.append("第" + paginator.getPageIndex() + "页");
			buffer.append("/共" + totalPage + "页");

			buffer.append("</td>\n");
			if (paginator.getPageIndex() > 1) {// 不是第一页，显示“第一页”，“上一页”链接
				buffer.append("<td width='1%' align=center><a href='javascript:GotoPage(1)'"
						+ "onMouseover='javascript:firstOver();'"
						+ "onMouseOut='javascript:firstOut();'"
						+ "><img name='first' id='first' src='" + path
						+ "/images/page/first.gif' border='0' ></a></td>\n");
				buffer.append("<td width='1%' align=center><a href='javascript:GotoPage("
						+ (paginator.getPageIndex() - 1) + ")'"
						+ "onMouseover='javascript:prevOver();'"
						+ "onMouseOut='javascript:prevOut();'" + ">" +

						"<img name='prev' id='prev' src='" + path
						+ "/images/page/prev.gif' border='0' ></a></td>\n");
			} else {// 是第一页，不显示“第一页”，“上一页”链接
				buffer.append("<td width='1%' align=center><img name='first' id='first' src='"
						+ path + "/images/page/first3.gif' border='0' ></td>\n");
				buffer.append("<td width='1%' align=center><img name='prev' id='prev' src='" + path
						+ "/images/page/prev3.gif' border='0' ></td>\n");
			}
			if (paginator.getPageIndex() == totalPage || totalPage <= 1) {// 是最后页或共只有一页，不显示“下一页”，“最后页”链接
				buffer.append("<td width='1%' align=center><img name='next' id='next' src='" + path
						+ "/images/page/next3.gif' border='0' ></td>\n");
				buffer.append("<td width='1%' align=center><img name='last' id='last' src='" + path
						+ "/images/page/last3.gif' border='0' ></td>\n");
			} else {// 不是最后页且总页数大于1页，显示“下一页”，“最后页”链接
				buffer.append("<td width='1%' align=center><a href='javascript:GotoPage("
						+ (paginator.getPageIndex() + 1) + ")'"
						+ "onMouseover='javascript:nextOver();'"
						+ "onMouseOut='javascript:nextOut();'"
						+ "><img name='next' id='next' src='" + path
						+ "/images/page/next.gif' border='0' ></a></td>\n");
				buffer.append("<td width='1%' align=center><a href='javascript:GotoPage("
						+ totalPage + ")'" + "onMouseover='javascript:lastOver();'"
						+ "onMouseOut='javascript:lastOut();'"
						+ "><img name='last' id='last' src='" + path
						+ "/images/page/last.gif' border='0' ></a></td>\n");
			}

			buffer
					.append("<td width='10%' align='right'>每页<input type='text' style='width:32px;' size='2' name='pageSize' class='loginbd2' value='"
							+ paginator.getPageSize()
							+ "' onblur='checkPageSize(document.all.pageIndex.value,document.all.pageSize.value,"
							+ paginator.getTotalCount() + ")'>条</td>\n");

			buffer
					.append("<td width='10%' align='center'>到第<input type='text' style='width:30px;' size='2' name='pageIndex' class='loginbd2' value='"
							+ paginator.getPageIndex()
							+ "' onblur='checkPage(document.all.pageIndex.value,document.all.pageSize.value,"
							+ paginator.getTotalCount() + ")'>页</td>\n");

			buffer
					.append("<td width='5%' align='left'><a href='javascript:GotoPage(document.all.pageIndex.value);'"
							+ "onMouseover='javascript:goOver();'"
							+ "onMouseOut='javascript:goOut();'>"
							+ "<img name='goPage' id='goPage' src='"
							+ path
							+ "/images/page/go.gif' border='0'></a></td>\n");

			buffer.append("</tr>\n");
			buffer.append("<input type='hidden' name='pageRequest' value=''>\n");
			buffer.append("</table>\n");
			/*
			 * } else {// 无记录 buffer.append("<table width='100%' border='0' align=right>\n");
			 * buffer.append("<th>无记录</th>"); buffer.append("<tr>\n"); buffer.append("<td width='20%'></td>\n");
			 * buffer.append("<td width='10%' align='center'>\n" + 0 + "条记录"); buffer.append("</td>\n");
			 * buffer.append("<td width='12%' align='center'>\n"); buffer.append("第" + 0+ "页");
			 * buffer.append("/共" + 0 + "页"); buffer.append("</td>\n"); buffer.append("<td width='1%' align=center><img
			 * name='first' id='first' src='../images/page/first3.gif' border='0' ></td>\n");
			 * buffer.append("<td width='1%' align=center><img name='prev' id='prev'
			 * src='../images/page/prev3.gif' border='0' ></td>\n"); buffer.append("<td width='1%' align=center><img
			 * name='next' id='next' src='../images/page/next3.gif' border='0' ></td>\n");
			 * buffer.append("<td width='1%' align=center><img name='last' id='last'
			 * src='../images/page/last3.gif' border='0' ></td>\n"); buffer.append("<td width='10%' align='center'>每页<input
			 * type='text' size='3' name='pageSize' value='" +paginator.getPageSize()+"'
			 * onblur='checkPageSize(document.all.pageIndex.value,document.all.pageSize.value,"+paginator.getTotalCount()+")'>条</td>\n");
			 * buffer.append("<td width='10%' align='center'>到第<input type='text' size='4'
			 * name='pageIndex' value='" + paginator.getPageIndex() + "'
			 * onblur='checkPage(document.all.pageIndex.value,document.all.pageSize.value,"+paginator.getTotalCount()+")'>页</td>\n");
			 * buffer.append("<td width='1%' align='center'><a
			 * href='javascript:GotoPage(document.all.pageIndex.value);'><img
			 * src='../images/go.gif' border='0'></a></td>\n"); buffer.append("</tr>\n");
			 * buffer.append("<input type='hidden' name='pageRequest' value=''>\n");
			 * buffer.append("</table>\n"); }
			 */
			out.write(buffer.toString());
			out.flush();
		} catch (Exception e) {
		}

		return EVAL_PAGE;
	}

	public String getPaginatorStr(Paginator paginator) throws JspTagException {
		if (ps == null || "".equals(ps)) {
			ps = "15";
		}

		StringBuffer buffer = new StringBuffer(1000);
		if (paginator == null) {
			paginator = new Paginator();
		}
		int totalPage = paginator.getTotalPage();
		buffer.append("<script language=\"JavaScript\">\n");
		buffer.append("<!--\n");
		buffer.append("function GotoPage(id) {\n");
		buffer.append("document.all.pageRequest.value=1;\n");
		buffer.append("if(id<=0){id=1;}\n");
		buffer.append("document.all.pageIndex.value=id;\n");
		buffer.append("document.forms[0].submit();\n");
		buffer.append("}\n");

		buffer.append("function checkPageSize(page,size,total){\n");
		buffer.append("var reg = /[^0-9]/;\n");
		buffer.append("if(size.match(reg) != null){\n");
		buffer.append("alert('每页记录数请使用数字');\n");
		buffer.append("document.all.pageSize.focus();}\n");
		buffer.append("if(size<1){\n");
		buffer.append("document.all.pageSize.value=" + ps + ";}\n");
		buffer.append("if(size>5000){\n");
		buffer.append("document.all.pageSize.value=5000;}\n");
		buffer.append("var newsize = document.all.pageSize.value;\n");
		buffer.append("checkPage(page,newsize,total);\n");
		buffer.append("}\n");

		buffer.append("function goOver(){\n");
		buffer.append("document.all.goPage.src='../images/page/go2.gif';\n");
		buffer.append("return false;\n");
		buffer.append("}\n");
		buffer.append("function goOut(){\n");
		buffer.append("document.all.goPage.src='../images/page/go.gif';\n");
		buffer.append("return false;\n");
		buffer.append("}\n");

		buffer.append("function firstOver(){\n");
		buffer.append("document.all.first.src='../images/page/first2.gif';\n");
		buffer.append("return false;\n");
		buffer.append("}\n");
		buffer.append("function firstOut(){\n");
		buffer.append("document.all.first.src='../images/page/first.gif';\n");
		buffer.append("return false;\n");
		buffer.append("}\n");

		buffer.append("function prevOver(){\n");
		buffer.append("document.all.prev.src='../images/page/prev2.gif';\n");
		buffer.append("return false;\n");
		buffer.append("}\n");
		buffer.append("function prevOut(){\n");
		buffer.append("document.all.prev.src='../images/page/prev.gif';\n");
		buffer.append("return false;\n");
		buffer.append("}\n");

		buffer.append("function nextOver(){\n");
		buffer.append("document.all.next.src='../images/page/next2.gif';\n");
		buffer.append("return false;\n");
		buffer.append("}\n");
		buffer.append("function nextOut(){\n");
		buffer.append("document.all.next.src='../images/page/next.gif';\n");
		buffer.append("return false;\n");
		buffer.append("}\n");

		buffer.append("function lastOver(){\n");
		buffer.append("document.all.last.src='../images/page/last2.gif';\n");
		buffer.append("return false;\n");
		buffer.append("}\n");
		buffer.append("function lastOut(){\n");
		buffer.append("document.all.last.src='../images/page/last.gif';\n");
		buffer.append("return false;\n");
		buffer.append("}\n");

		buffer.append("function checkPage(page,size,total){\n");
		buffer.append("var reg = /[^0-9]/;\n");
		buffer.append("if(page.match(reg) != null){\n");
		buffer.append("alert('跳转页数请使用数字');\n");
		buffer.append("document.all.pageIndex.focus();}\n");
		buffer.append("var num = total/size;  \n");
		buffer.append("var nums = Math.round(num+0.4999999);\n");
		buffer.append("if(page<1){\n");
		buffer.append("document.all.pageIndex.value=1;}\n");
		buffer.append("if(page > nums){\n");
		buffer.append("document.all.pageIndex.value=nums;}\n");
		buffer.append("}\n");

		buffer.append("//-->\n");
		buffer.append("</script>\n");
		buffer.append("<script language=\"JavaScript\" event=\"onkeydown\" for=\"document\">\n");
		buffer.append("if(event.srcElement.name=='pageSize'){\n");
		buffer
				.append("if(((event.keyCode>=48)&&(event.keyCode<=57))||((event.keyCode>=96)&&(event.keyCode<=105))||(event.keyCode==8)||(event.keyCode==46)||(event.keyCode==13)||(event.keyCode==9)){\n");
		buffer.append("return true;\n");
		buffer.append("}else{return false;}}");
		buffer.append("if(event.srcElement.name=='pageIndex'){\n");
		buffer
				.append("if(((event.keyCode>=48)&&(event.keyCode<=57))||((event.keyCode>=96)&&(event.keyCode<=105))||(event.keyCode==8)||(event.keyCode==46)||(event.keyCode==13)||(event.keyCode==9)){\n");
		buffer.append("return true;\n");
		buffer.append("}else{return false;}}");
		buffer.append("</script>\n");
		// 以上为页面跳转的JavaScript方法

		buffer.append("<table width='100%' border='0' align='right'>\n");
		buffer.append("<tr>\n");
		buffer.append("<td width='5%' align='center'></td>\n");
		buffer.append("<td width='10%' align='right'>\n" + paginator.getTotalCount() + "条记录");
		buffer.append("</td>\n");
		buffer.append("<td width='12%' align='center'>\n");
		buffer.append("第" + paginator.getPageIndex() + "页");
		buffer.append("/共" + totalPage + "页");

		buffer.append("</td>\n");
		if (paginator.getPageIndex() > 1) {// 不是第一页，显示“第一页”，“上一页”链接
			buffer
					.append("<td width='1%' align=center><a href='javascript:GotoPage(1)'"
							+ "onMouseover='javascript:firstOver();'"
							+ "onMouseOut='javascript:firstOut();'"
							+ "><img name='first' id='first' src='../images/page/first.gif' border='0' ></a></td>\n");
			buffer
					.append("<td width='1%' align=center><a href='javascript:GotoPage("
							+ (paginator.getPageIndex() - 1) + ")'"
							+ "onMouseover='javascript:prevOver();'"
							+ "onMouseOut='javascript:prevOut();'" + ">" +

							"<img name='prev' id='prev' src='../images/page/prev.gif' border='0' ></a></td>\n");
		} else {// 是第一页，不显示“第一页”，“上一页”链接
			buffer
					.append("<td width='1%' align=center><img name='first' id='first' src='../images/page/first3.gif' border='0' ></td>\n");
			buffer
					.append("<td width='1%' align=center><img name='prev' id='prev' src='../images/page/prev3.gif' border='0' ></td>\n");
		}
		if (paginator.getPageIndex() == totalPage || totalPage <= 1) {// 是最后页或共只有一页，不显示“下一页”，“最后页”链接
			buffer
					.append("<td width='1%' align=center><img name='next' id='next' src='../images/page/next3.gif' border='0' ></td>\n");
			buffer
					.append("<td width='1%' align=center><img name='last' id='last' src='../images/page/last3.gif' border='0' ></td>\n");
		} else {// 不是最后页且总页数大于1页，显示“下一页”，“最后页”链接
			buffer
					.append("<td width='1%' align=center><a href='javascript:GotoPage("
							+ (paginator.getPageIndex() + 1)
							+ ")'"
							+ "onMouseover='javascript:nextOver();'"
							+ "onMouseOut='javascript:nextOut();'"
							+ "><img name='next' id='next' src='../images/page/next.gif' border='0' ></a></td>\n");
			buffer
					.append("<td width='1%' align=center><a href='javascript:GotoPage("
							+ totalPage
							+ ")'"
							+ "onMouseover='javascript:lastOver();'"
							+ "onMouseOut='javascript:lastOut();'"
							+ "><img name='last' id='last' src='../images/page/last.gif' border='0' ></a></td>\n");
		}

		buffer
				.append("<td width='10%' align='right'>每页<input type='text' style='width:32px;' size='2' name='pageSize' value='"
						+ paginator.getPageSize()
						+ "' onblur='checkPageSize(document.all.pageIndex.value,document.all.pageSize.value,"
						+ paginator.getTotalCount() + ")'>条</td>\n");

		buffer
				.append("<td width='10%' align='center'>到第<input type='text' style='width:30px;' size='2' name='pageIndex' value='"
						+ paginator.getPageIndex()
						+ "' onblur='checkPage(document.all.pageIndex.value,document.all.pageSize.value,"
						+ paginator.getTotalCount() + ")'>页</td>\n");

		buffer
				.append("<td width='5%' align='left'><a href='javascript:GotoPage(document.all.pageIndex.value);'"
						+ "onMouseover='javascript:goOver();'"
						+ "onMouseOut='javascript:goOut();'>"
						+ "<img name='goPage' id='goPage' src='../images/page/go.gif' border='0'></a></td>\n");

		buffer.append("</tr>\n");
		buffer.append("<input type='hidden' name='pageRequest' value=''>\n");
		buffer.append("</table>\n");

		return buffer.toString();
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setScope(String scope) {
		this.scope = scope;
	}
}
